Skip to content

approx: expose pytest.Approx as the return type of pytest.approx#14647

Open
bluetech wants to merge 7 commits into
pytest-dev:mainfrom
bluetech:approx-typing
Open

approx: expose pytest.Approx as the return type of pytest.approx#14647
bluetech wants to merge 7 commits into
pytest-dev:mainfrom
bluetech:approx-typing

Conversation

@bluetech

Copy link
Copy Markdown
Member

As requested in #14164 (part of #7469).

There are some initial commits that are prerequisites or small cleanups/refactors to the code.

bluetech added 7 commits June 23, 2026 14:30
I don't think it has any effect (always False, checked two lines above).
Probably a copy/paste error.
Consistently use `_is_bool` check which handles `numpy.bool` in addition
to `bool`, instead of `isinstance(..., bool)`.
The code is more direct this way, and more amenable to typing (in follow
up commits).
Not really helpful and annoying to type.
This (tries to) improve the typing of the approx code in preparation of
exposing a type for its return value.

The main change is to make `ApproxBase` generic in the expected value
type, so that `self.expected` can have a correct type. This is not so
easy...
The `python_api.py` file used to also contain the implementation of
`raises`, but that was moved to its own file a while ago. Now
`python_api.py` only houses the `approx` code, so let's rename it to the
more obvious and less generic `approx.py`.
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jun 23, 2026

@nicoddemus nicoddemus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

Comment thread src/_pytest/python_api.py
__hash__ = None # type: ignore

def __ne__(self, actual) -> bool:
def __ne__(self, actual: object) -> bool:

@nicoddemus nicoddemus Jun 24, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can drop this __ne__ implementation, the default implementation (when __eq__ is implemented but __ne__ is not) does exactly this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants